package com.gitee.wsl.doc.prosemirror.ui.ext

import com.gitee.wsl.doc.prosemirror.model.content.ResolvedPos
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.state.selection.TextSelection
import com.gitee.wsl.doc.prosemirror.ui.EditorView
import com.gitee.wsl.ext.base.ifValue


fun atBlockStart(state: EditorState, view: EditorView?): ResolvedPos? {
    val cursor = (state.selection as TextSelection?)?._cursor
    if (cursor == null || (view == null).ifValue( cursor.parentOffset > 0,view!!.endOfTextblock(state,TextblockDir.DirHorizontal.backward )))
         return null
    return cursor
}

/**
 * 检查光标是否位于文本块的末尾
 * @param state 编辑器状态
 * @param view 编辑器视图（可选）
 * @return 光标位置或null
 */
fun atBlockEnd(state: EditorState, view: EditorView? = null): ResolvedPos? {
    val cursor = (state.selection as TextSelection)._cursor ?: return null

    val isAtEnd = view?.endOfTextblock(state, TextblockDir.DirHorizontal.forward )
                       ?: (cursor.parentOffset >= cursor.parent.content.size)

    return if (isAtEnd) cursor else null
}





